<!DOCTYPE html>
<html class="x-admin-sm">
    <head>
        <meta charset="UTF-8">
        <title>欢迎页面-X-admin2.2</title>
        <meta name="renderer" content="webkit">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
        <link rel="stylesheet" href="../../static/css/font.css">
        <link rel="stylesheet" href="../../static/css/xadmin.css">
        <script src="../../static/lib/layui/layui.js" charset="utf-8"></script>
        <script type="text/javascript" src="../../static/js/xadmin.js"></script>
        <!--[if lt IE 9]>
          <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
          <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
        <![endif]-->
    </head>
    <body>
        <div class="x-nav">
          <span class="layui-breadcrumb">
            <a href="">首页</a>
            <a href="">管理员管理</a>
            <a>
              <cite>管理员列表</cite></a>
          </span>
          <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" onclick="location.reload()" title="刷新">
            <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i></a>
        </div>
        <div class="layui-fluid">
            <div class="layui-row layui-col-space15">
                <div class="layui-col-md12">
                    <div class="layui-card">
                        <div class="layui-card-body ">
                            <form class="layui-form layui-col-space5">
                                <div class="layui-inline layui-show-xs-block">
                                    <input class="layui-input"  autocomplete="off" placeholder="开始日" name="start" id="start">
                                </div>
                                <div class="layui-inline layui-show-xs-block">
                                    <input class="layui-input"  autocomplete="off" placeholder="截止日" name="end" id="end">
                                </div>
                                <div class="layui-inline layui-show-xs-block">
                                    <input type="text" name="username"  placeholder="请输入用户名" autocomplete="off" class="layui-input">
                                </div>
                                <div class="layui-inline layui-show-xs-block">
                                    <button class="layui-btn"  lay-submit="" lay-filter="sreach"><i class="layui-icon">&#xe615;</i></button>
                                </div>
                            </form>
                        </div>
                        <div class="layui-card-header">
                            <button class="layui-btn layui-btn-danger" onclick="delAll()"><i class="layui-icon"></i>批量删除</button>
                            <button class="layui-btn" onclick="xadmin.open('添加用户','/AdminAccountAdd/',600,400)"><i class="layui-icon"></i>添加</button>
                        </div>
                        <div class="layui-card-body" id="admin" lay-filter="admin">
                        </div>
                    </div>
                </div>
            </div>
        </div> 
    </body>
    <script>
      layui.use(['laydate','form', 'table'], function(){
        let laydate = layui.laydate, form = layui.form, table = layui.table;
        
        //执行一个laydate实例
        laydate.render({
          elem: '#start' //指定元素
        });

        //执行一个laydate实例
        laydate.render({
          elem: '#end' //指定元素
        });



        table.render({
          elem: '#admin'
          , height: 600
          , url: '/AdminAccount/' //数据接口
          , method: 'post'  //
          , page: true //开启分页
          , cols: [[ //表头
              {checkbox: true}
              , {field: 'id', title: 'ID'}
              , {field: 'username', title: '登录名'}
              , {field: 'email', title: '邮箱'}
              , {field: 'create_time', title: '创建时间'}
              , {field: 'is_superuser', title: '超级管理员'}
              , {title: '操作', fixed: 'right', width: '25%', align: 'center', toolbar: '#bar'}
          ]]
        });
        table.on('tool(admin)', function (obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
              let data = obj.data; //获得当前行数据
              let layEvent = obj.event; //获得 lay-event 对应的值
              let tr = obj.tr; //获得当前行 tr 的DOM对象
              if (layEvent === 'unauth') {
                  layer.confirm('确认解绑两步验证', function (index) {
                      layer.close(index);
                      //向服务端发送删除指令
                      $.post(
                          "/Delete2faAuth/",
                          data,
                          function (res) {
                              layer.msg(res.data, {time: 2000});
                          },
                          'json'
                      );
                  });
              }
              else if (layEvent === 'del') { //删除
                  layer.confirm('真的删除行么', function (index) {
                      obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                      layer.close(index);
                      //向服务端发送删除指令
                      $.post(
                          "/AdminAccountDel/",
                          data,
                          function (res) {
                              layer.msg(res.message, {time: 2000});
                          },
                          'json'
                      );
                  });
              } else if (layEvent === 'edit') { //编辑
                  //do something
                  let htmlData = '<div class="layui-fluid">\n' +
                      '            <div class="layui-row">\n' +
                      '                <form class="layui-form">\n' +
                      '                  <div class="layui-form-item">\n' +
                      '                      <label for="username" class="layui-form-label">\n' +
                      '                          <span class="x-red">*</span>登录名\n' +
                      '                      </label>\n' +
                      '                      <div class="layui-input-inline">\n' +
                      '                          <input type="text" id="username" name="username" readonly="readonly" value="' + data.username+'"required="" lay-verify="required"\n' +
                      '                          autocomplete="off" class="layui-input">\n' +
                      '                      </div>\n' +
                      '                  </div>\n' +
                      '                  <div class="layui-form-item">\n' +
                      '                      <label for="L_email" class="layui-form-label">\n' +
                      '                          <span class="x-red">*</span>邮箱\n' +
                      '                      </label>\n' +
                      '                      <div class="layui-input-inline">\n' +
                      '                          <input type="text" id="L_email" name="email" required="" value="' + data.email + '"lay-verify="email"\n' +
                      '                          autocomplete="off" class="layui-input">\n' +
                      '                      </div>\n' +
                      '                      <div class="layui-form-mid layui-word-aux">\n' +
                      '                          <span class="x-red">*</span>\n' +
                      '                      </div>\n' +
                      '                  </div>\n' +
                      '                  <div class="layui-form-item">\n' +
                      '                      <label for="L_pass" class="layui-form-label">\n' +
                      '                          <span class="x-red">*</span>密码\n' +
                      '                      </label>\n' +
                      '                      <div class="layui-input-inline">\n' +
                      '                          <input type="password" id="L_pass" name="pass" required="" lay-verify="pass"\n' +
                      '                          autocomplete="off" class="layui-input">\n' +
                      '                      </div>\n' +
                      '                  </div>\n' +
                      '              </form>\n' +
                      '            </div>\n' +
                      '        </div>';
                  layer.open({
                      title: "更新",
                      area: ["400px", "400px"],
                      type: 1,
                      content: htmlData,//把数据显示在弹出层上
                      btn: ['确定', '我再想想'],
                      success: function (layero, index){
                        $(':focus').blur();
                        // 添加form标识
                        layero.addClass('layui-form');
                        // 将保存按钮改变成提交按钮
                        layero.find('.layui-layer-btn0').attr({
                            'lay-filter': 'update',
                            'lay-submit': ''
                        });

                        form.verify({
                            pass: [/(.+){6,12}$/, '密码必须6到12位']
                        });
                      },
                      yes: function (index, layero) {//改完，点击确定
                          form.on('submit(update)', function (data) {
                              let username = layero.find("#username").val();//获得修改后的值
                              let re_email = layero.find("#L_email").val();
                              let re_pass = layero.find("#L_pass").val();
                              console.log(re_email);
                              //同步更新缓存对应的值
                              $.post(
                                  "/AdminAccountUpdate/",
                                  {username: username, email: re_email,pass: re_pass},
                                  function (res) {
                                      layer.msg(res.message, {time: 2000});
                                      if (res.code === 0) {
                                          obj.update({
                                              email: re_email,
                                          });
                                          layer.close(index);
                                      }
                                  },
                                  'json'
                              );
                          });
                      }
                  });
              }
          });

      });



      function delAll (argument) {

        var data = tableCheck.getData();
  
        layer.confirm('确认要删除吗？'+data,function(index){
            //捉到所有被选中的，发异步进行删除
            layer.msg('删除成功', {icon: 1});
            $(".layui-form-checked").not('.header').parents('tr').remove();
        });
      }
    </script>
    <script type="text/html" id="bar">
        <a class="layui-btn layui-btn-xs" lay-event="unauth">解绑两步验证</a>
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>

    </script>
</html>